package com.yj.consumer.exception;

import org.springframework.validation.BindException;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.List;

@RestControllerAdvice
public class MyException {


    @ExceptionHandler(Exception.class)
    public String handlerBindException(Exception e){
        System.out.println(e.getMessage());
        if (e instanceof Exception){
            BindException ex = (BindException) e;
            List<ObjectError> list = ex.getAllErrors();
            list.forEach(x->{
                System.out.println(x);
            });
            ObjectError objectError = list.get(0);
            return objectError.getDefaultMessage();
        }

        return "system error";
    }

}
